import { GCellRenderer } from "../../../../com/mui/controls/GCellRenderer";
import { ObjCreate } from "../common/ObjCreate";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { Language } from "../../../../extend/language/Language";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { AvatarInfo } from "../../resource/info/AvatarInfo";
import { Cache } from "../../cache/Cache";
import { AvatarType } from "../../resource/info/AvatarType";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { ChangeHeadIconConfig } from "../../resource/ChangeHeadIconConfig";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { PulseSharedObject } from "../player/PulseSharedObject";
import { Alert } from "../../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../common/alertwins/CheckBoxWin";
import { ClockManager } from "../../manager/ClockManager";
import { Global } from "../../../../com/gengine/global/Global";
import { GameConst } from "../../../component/gconst/GameConst";
import { MsgManager } from "../../manager/MsgManager";
import { GButton } from "../../../../com/mui/controls/GButton";
type int = number;
//class HeadIconCellRenderer
    
    export  class HeadIconCellRenderer extends GCellRenderer
    {
        constructor()
        {
            super();
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            ObjCreate.createBg(0, 0, 98, 104, this);
            var loc1=new Object /* flash.display.Bitmap */();
            loc1.x = 0;
            loc1.y = 3;
            this.addChild(loc1);
            LoaderHelp.setBitmapdata("Friend_Bg_Head.swf", loc1);
            this._headImage = new Object /* flash.display.Bitmap */();
            this._headImage.x = 17;
            this._headImage.y = 20;
            this.addChild(this._headImage);
            this._replaceBtn = ObjCreate.createGButton(Language.getString(70506), 58, 81, 38, 20, this);
            this._replaceBtn.drawNow();
            this._replaceBtn.visible = false;
            this._replaceBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onReplaceBtnClick);
            this._notOpenTx = ObjCreate.createTextField(Language.getString(70507), 34, 32, 32, 40, this, GlobalStyle.textFormat1);
            var loc2;
            this._notOpenTx.multiline = loc2 = true;
            this._notOpenTx.wordWrap = loc2;
            this._notOpenTx.visible = false;
            this._nameTx = ObjCreate.createTextField("", 2, 0, 96, 20, this, new GTextFormat(FontUtil.songtiName, 12, 15859633, null, null, null, null, null, flash.text.TextFormatAlign.CENTER));
            this._nameTx.autoSize = flash.text.TextFormatAlign.CENTER;
            this._selectLine = new Object /* flash.display.Sprite */();
            this._selectLine.mouseEnabled = false;
            this._selectLine.graphics.lineStyle(2, 16776960, 1);
            this._selectLine.graphics.drawRoundRect(1, 1, width - 2, height - 2, 6, 6);
            this._selectLine.graphics.endFill();
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            var arg0: Object;
            var str: string;

            var loc1;
            str = null;
            arg0 = arg1;
            super.data = arg0;
            if (!arg0 || arg0.data == null) 
            {
                this._flag = false;
                this._replaceBtn.visible = false;
                this._notOpenTx.visible = true;
                this._nameTx.text = "";
                this._headImage.bitmapData = null;
            }
            else 
            {
                this._flag = true;
                this._avatarId = arg0.data as int;
                this._notOpenTx.visible = false;
                this._replaceBtn.visible = true;
                if (this._avatarId != 0) 
                {
                    AvatarInfo.getAvatar(Cache.instance.role.entityInfo.camp, Cache.instance.role.entityInfo.sex, AvatarType.BIG, (arg1: ImageInfo): void
                    {
                        _headImage.bitmapData = arg1.bitmapData;
                        return;
                    }, this._avatarId)
                    this._nameTx.text = ChangeHeadIconConfig.instance.getHeadIconName(this._avatarId);
                }
                else 
                {
                    AvatarInfo.getAvatar(Cache.instance.role.entityInfo.camp, Cache.instance.role.entityInfo.sex, AvatarType.BIG, (arg1: ImageInfo): void
                    {
                        _headImage.bitmapData = arg1.bitmapData;
                        return;
                    })
                    str = "" + Cache.instance.role.entityInfo.camp + Cache.instance.role.entityInfo.sex;
                    this._nameTx.text = GameDefConfig.instance.getOldHeadIconName(str);
                }
            }
            if (this._notOpenTx.visible) 
            {
                this.mouseEnabled = false;
                this.mouseChildren = false;
            }
            else 
            {
                this.mouseEnabled = true;
                this.mouseChildren = true;
            }
            return;
        }

        public /* override */ set selected(arg1: Boolean)
        {
            super.selected = arg1;
            if (arg1) 
            {
                if (this._flag) 
                {
                    addChild(this._selectLine);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ChangePrwHead, this._avatarId));
                }
                else if (this._selectLine.parent) 
                {
                    removeChild(this._selectLine);
                }
            }
            else if (this._selectLine.parent) 
            {
                removeChild(this._selectLine);
            }
            return;
        }

        protected onReplaceBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=!PulseSharedObject.isTodayNotTips("ChangeHeadIcon");
            if (loc1) 
            {
                Alert.alertWinRenderer = CheckBoxWin;
                Alert.show(Language.getStringByParam(70508, this._nameTx.text), null, Alert.OK | Alert.CANCEL, null, this.onBtnClickHandler);
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChangeHeadIconGoReplace, this._avatarId));
            }
            return;
        }

        /* internal  */onBtnClickHandler(arg1: int, arg2: Boolean): void
        {
            var loc1=ClockManager.instance.nowDate.getTime();
            if (!Global.isDebugModle) 
            {
                if (Cache.instance.role.changeHeadIconTime && Cache.instance.role.changeHeadIconTime.getTime() + GameConst.UpdateAvatarInterval * 1000 > loc1) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(70509));
                    return;
                }
                if (GameConst.UpdateAvatarCount - Cache.instance.role.changeHeadIconCount <= 0) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(70510));
                    return;
                }
            }
            if (arg1 == Alert.OK) 
            {
                Alert.resetToDefault();
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChangeHeadIconGoReplace, this._avatarId));
            }
            if (arg2) 
            {
                PulseSharedObject.save("ChangeHeadIcon", true);
            }
            return;
        }

        /* internal */ /* var */_headImage: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_replaceBtn: GButton;

        /* internal */ /* var */_avatarId: int;

        /* internal */ /* var */_notOpenTx: Object /* flash.text.TextField */;

        /* internal */ /* var */_nameTx: Object /* flash.text.TextField */;

        protected /* var */_selectLine: Object /* flash.display.Sprite */;

        /* internal */ /* var */_flag: Boolean;
    }
